package com.coder.algorithm.leetcode

import com.coder.algorithm.struct.ListNode

/**
 * [141. 环形链表](https://leetcode-cn.com/problems/linked-list-cycle/)
 * Example:
 * var li = ListNode(5)
 * var v = li.`val`
 * Definition for singly-linked list.
 * class ListNode(var `val`: Int) {
 *     var next: ListNode? = null
 * }
 *
 * @author yuhus
 * @date: 2022/3/31
 */
object l_0141 {
    fun hasCycle(head: ListNode?): Boolean {
        if (head == null) {
            return false
        }
        var slow = head.next
        var fast = head.next?.next
        while (fast != null) {
            if (fast == slow) {
                return true
            }
            slow = slow?.next
            fast = fast?.next?.next
        }
        return false
    }
}